package com.atguigu.lease.web.app.custom.interceptor;

import com.atguigu.lease.common.annotation.CheckPermission;
import com.atguigu.lease.common.context.LoginUserContext;
import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
public class AuthorizationInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {

        String phone = LoginUserContext.getLoginUser().getUsername();
        if ("13888888888".equals(phone)) {
            CheckPermission permission = null;
            if (handler instanceof HandlerMethod handlerMethod) {
                permission = handlerMethod.getMethodAnnotation(CheckPermission.class);
            }
            if (permission == null) {
                return true;
            } else {
                throw new LeaseException(ResultCodeEnum.ADMIN_ACCESS_FORBIDDEN);
            }
        } else {
            return true;
        }
    }

}
